<?php
/**
*  
*  Create On 2013-9-10����12:00:57
*  Author lidianbin
*  QQ: 281443751
*  Email: lidianbin@iwind-tech.com
**/
include_once("phpanalysis/PhpAnalysis.class.php");
class StringUtil{
	
	/**
	 * 将html格式转成UTF8字符串。
	 * 包括：去除html标签、数学公式转化、去除换行、取200字符等
	 * @param unknown $string
	 * @return string
	 */
	public function html2utf8($string) {
		$t = strip_tags ($string);

		$t = mb_substr ( $t, 0, 300 );
		$t = preg_replace("/(\s|\&nbsp\;|　|\xc2\xa0|\&\;)/"," ",$t); //比较慢
		$t = str_ireplace ( "&nbsp;", " ", $t );
// 		$t = str_ireplace ( "&ldquo;", "\"", $t );
// 		$t = str_ireplace ( "&rdquo;", "\"", $t );
// 		$t = str_ireplace ( "&omega;", "Ω", $t );
// 		$t = str_ireplace ( "&pi;", "π", $t );
// 		$t = str_ireplace ( "&prime;", "'", $t );
// 		$t = str_ireplace ( "#39", "'", $t );
// 		$t = str_ireplace ( "&times;", "×", $t );
// 		$t = str_ireplace ( "&radic;", "ω", $t );
// 		$t = str_ireplace ( "&gt;", ">", $t );
// 		$t = str_ireplace ( "&lt;", "<", $t );
// 		$t = str_ireplace ( "&theta;", "Θ", $t );
// 		$t = str_ireplace ( "&deg;", "°", $t );
// 		$t = str_ireplace ( "&hellip;", "...", $t );
// 		$t = str_ireplace ( "&lambda;", "λ", $t );
// 		$t = str_ireplace ( "&mdash;", "-", $t );
// 		$t = str_ireplace ( "&rho;", "ρ", $t );
// 		$t = str_ireplace ( "&bull;", "/", $t );
// 		$t = str_ireplace ( "&alpha;", "α", $t );
// 		$t = str_ireplace ( "&beta;", "β", $t );
// 		$t = str_ireplace ( "&epsilon;", "ε", $t );
// 		$t = str_ireplace ( "&minus;", "-", $t );
		
		$t = str_replace ( "\t", "", $t );
		$t = str_replace ( "\n", "", $t );
		$t = str_replace ( "\r", "", $t );
		$t = preg_replace("/(\s+)/"," ",$t);
		$t = mb_substr ( $t, 0, 200 );
		
		return $t;
	}
	
	//截取字符串
	public function cut_str_utf($string, $length,$adder="…") {
		preg_match_all("/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/", $string, $info);
		$wordscut="";
		$j=0;
		for($i=0; $i<count($info[0]); $i++) {
			$wordscut .= $info[0][$i];
			$j = ord($info[0][$i]) > 127 ? $j + 2 : $j + 1;
			if ($j > $length - 1) {
				return $wordscut.$adder;
			}
		}
		return join('', $info[0]);
	}
	
	/**
	 * 分词
	 * @param unknown_type $str
	 */
	public function split_word($str){
		if($str!=""){
			$pa = new PhpAnalysis();
			$pa->SetSource($str);
			//设置分词属性
			$pa->resultType = 1;
			$pa->differMax  = true;
			$pa->StartAnalysis();
			//获取你想要的结果
			$str = $pa->GetFinallyResult(",");
		}
		return $str;
		
	}
	
	public function is_email($val){
		$reg='/^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/';
		return preg_match($reg, $val);
	}
	
	public function is_phone($val){
		return preg_match('/^1[3458][0-9]{9}$/',$val);
	}
	
	public function is_password($val){
		return preg_match('/^[a-zA-Z0-9_]{6,18}$/',$val);
	}
	
}
